#include "mycoin.h"
#include <QPixmap>
#include <QDebug>
MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
{

}
MyCoin::MyCoin(QString img)
{
    win = false;
    if(img!="")
    {
        QPixmap pix;
        if(pix.load(img))
        {
            this->setFixedSize(pix.width(),pix.height());
            this->setStyleSheet("QPushButton{border:0px;}");
            this->setIconSize(QSize(pix.width(),pix.height()));
            this->setIcon(pix);
        }
        timer1 = new QTimer(this);
        timer2 = new QTimer(this);
        min = 1;
        max = 8;
        //监听定时器超时信号,30毫秒切换一张图片，切换到最后一张的时候停止这个计时器
        connect(timer1,&QTimer::timeout,[=](){
            QPixmap pix;
            QString pstr = QString(":/res/Coin000%1.png").arg(this->min++);
            if(pix.load(pstr))
            {
                this->setFixedSize(pix.width(),pix.height());
                this->setStyleSheet("QPushButton{border:0px;}");
                this->setIconSize(QSize(pix.width(),pix.height()));
                this->setIcon(pix);
            }
            if(this->min>=8)
            {
                this->min = 1;
                timer1->stop();
            }
        });
        connect(timer2,&QTimer::timeout,[=](){
            QPixmap pix;
            QString pstr = QString(":/res/Coin000%1.png").arg(this->max--);
            if(pix.load(pstr))
            {
                this->setFixedSize(pix.width(),pix.height());
                this->setStyleSheet("QPushButton{border:0px;}");
                this->setIconSize(QSize(pix.width(),pix.height()));
                this->setIcon(pix);
            }
            if(this->max<=1)
            {
                this->max = 8;
                timer2->stop();
            }
        });
    }
}

void MyCoin::setPx(int x)
{
    this->posx = x;
}
void MyCoin::setPy(int y )
{
    this->posy = y;
}
void MyCoin::setFlag(bool flag)
{
    this->flag = flag;
}
int MyCoin::getPx()
{
    return  posx;
}
int MyCoin::getPy()
{
    return posy;
}
void MyCoin::isWin()
{
    this->win = true;
}
QString MyCoin::print()
{
    return QString("x=%1 y=%2 flag=%3").arg(posx).arg(posy).arg(flag);
}
void MyCoin::changeflag()
{
    if(this->flag)
    {
        //正面
        timer1->start(30);
        this->flag = false;
    }else
    {
        //反面
        timer2->start(30);
        this->flag = true;
    }
}

void MyCoin::mousePressEvent(QMouseEvent* ev)
{
    if(this->win)
    {
        qDebug()<<"already win";
    }else
    {
        return QPushButton::mousePressEvent(ev);
    }

}
